package com.baizhi.wj.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.baizhi.wj.dto.VideoDTO;
import com.baizhi.wj.entity.Video;
import com.baizhi.wj.ov.VideoOV;
import com.baizhi.wj.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.xml.crypto.Data;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("video")
@CrossOrigin
public class VideoController {
    @Autowired
    private VideoService videoService;
    @RequestMapping("queryAllPage")
    @ResponseBody
    public VideoOV queryAllPage(@RequestBody VideoDTO videoDTO){
        return videoService.queryAll(videoDTO);
    }

    @RequestMapping("add")
    @ResponseBody
    public Map<String,String> add(@RequestBody Video video){
        Map<String,String> map = new HashMap<>();
        try {
            videoService.insert(video);
            map.put("message", "添加成功");
        } catch (Exception e){
            e.printStackTrace();
            map.put("message", "添加失败");
        }
        return map;
    }

    @RequestMapping("uploadHeadImg")
    @ResponseBody
    public Map<String,String> uploadHeadImg(MultipartFile videoFile,String oldImg){
        System.out.println("uploadHeadImg...oldImg..."+oldImg);
        Map<String,String> map = new HashMap<>();
        try {
            String path = videoService.uploadHeadImg(videoFile,oldImg);
            // System.out.println(path);
            map.put("fileName", path);
            map.put("message", "上传成功");
        } catch (Exception e){
            e.printStackTrace();
            map.put("message", "上传失败");
        }
        return map;
    }
    @RequestMapping("queryById")
    @ResponseBody
    public Video queryById(int id){
        return videoService.selectOne(id);
    }
    @RequestMapping("update")
    @ResponseBody
    public  Map<String,String>  update(@RequestBody Video video){
        Map<String,String> map = new HashMap<>();
        try {
            videoService.update(video);
            map.put("message","修改成功");
        }catch (Exception e){
            e.printStackTrace();
            map.put("message","修改失败");
        }
        return map;
    }
}
